import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';

import '../../../data/models/bt_friend.dart';
import '../../../widgets/pagination/bt_first_page_error_indicator.dart';
import '../../../widgets/pagination/bt_first_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_new_page_error_indicator.dart';
import '../../../widgets/pagination/bt_new_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_no_items_found_indicator.dart';
import '../../../widgets/pagination/bt_no_more_items_indicator.dart';
import '../models/bt_followed_list_model.dart';
import '../widgets/bt_followed_item_widget.dart';

class BtFollowedListScreen extends StatelessWidget {
  const BtFollowedListScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(0xFFFFFFFF),
      margin: const EdgeInsets.only(top: 8),
      child: ChangeNotifierProvider(
        create: (context) => BtFollowedListModel(),
        child: Consumer<BtFollowedListModel>(builder: (context, model, _) {
          return RefreshIndicator(
            onRefresh: () => Future.sync(
              () => model.pagingController.refresh(),
            ),
            child: MediaQuery.removePadding(
              context: context,
              removeTop: true,
              child: PagedListView<int, BtFriend>(
                pagingController: model.pagingController,
                builderDelegate: PagedChildBuilderDelegate<BtFriend>(
                  itemBuilder: (context, item, index) {
                    return BtFollowedItemWidget(
                      index: index,
                      item: item,
                      model: model,
                    );
                  },
                  firstPageErrorIndicatorBuilder: (_) => BtFirstPageErrorIndicator(
                    error: model.pagingController.error,
                    onTryAgain: () => model.pagingController.refresh(),
                  ),
                  newPageErrorIndicatorBuilder: (_) => BtNewPageErrorIndicator(
                    error: model.pagingController.error,
                    onTap: () => model.pagingController.retryLastFailedRequest(),
                  ),
                  firstPageProgressIndicatorBuilder: (_) => const BtFirstPageProgressIndicator(),
                  newPageProgressIndicatorBuilder: (_) => const Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    child: BtNewPageProgressIndicator(),
                  ),
                  noItemsFoundIndicatorBuilder: (_) => const Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    child: BtNoItemsFoundIndicator(),
                  ),
                  noMoreItemsIndicatorBuilder: (_) => const Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    child: BtNoMoreItemsIndicator(),
                  ),
                ),
              ),
            ),
          );
        }),
      ),
    );
  }
}
